package com.administrator.cloudapplication.tools;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.adapter.CarouselAdapter;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/4/14.
 */

public class BannerUtils {
    private OnPageSelected onPageSelected;

    public OnPageSelected getOnPageSelected() {
        return onPageSelected;
    }

    public void setOnPageSelected(OnPageSelected onPageSelected) {
        this.onPageSelected = onPageSelected;
    }

    public void initHeadView(List<String> mImages, final Activity activity,
                             final ViewPager viewPager) {
        onPageSelected.onSelected(0);
        final List<View> mvList = new ArrayList<>();
        for (int i = 0; i < mImages.size(); i++) {
            ViewGroup.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            ImageView imageView = new ImageView(activity);
            imageView.setLayoutParams(lp);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            Glide.with(activity).load(mImages.get(i)).placeholder(R.mipmap.error)
                    .error(R.mipmap.error).into(imageView);
            mvList.add(imageView);
            final int finalI = i;
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   onPageSelected.onImgClick(finalI);
                }
            });


            CarouselAdapter carouselAdapter = new CarouselAdapter(mvList);
            viewPager.setAdapter(carouselAdapter);
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    position = position % mvList.size();
                    onPageSelected.onSelected(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            int currentPosition = viewPager.getCurrentItem();

                            if (currentPosition == viewPager.getAdapter().getCount() - 1) {
                                // 最后一页
                                viewPager.setCurrentItem(0);
                            } else {
                                viewPager.setCurrentItem(currentPosition +1);
                            }
                        }

                    });
                }
            };
            Timer timer = new Timer();
            timer.schedule(timerTask, 3000, 3000);
        }
    }

    public interface OnPageSelected {
        void onSelected(int position);
        void onImgClick(int position);
    }
}
